package com.shushan.lockscreen;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.android.permission.FloatPermissionUtil;
import com.android.permission.rom.RomType;
import com.android.permission.rom.RomUtils;
import com.shushan.base.ActivityStackManager;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.ui.CustomDialog;
import com.shushan.ui.FloatToast;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.ToastUtils;

import static com.android.permission.rom.RomType.FUNTOUCHOS_ROM;
import static com.shushan.util.OpenSettingUtils.openVivoManager;

/**
 * @ClassName: LockScreenWindowManager
 * @Desciption: 锁屏窗口管理类
 * @author: yichaohua
 * @date: 2017-12-22
 */
public class LockScreenWindowManager {

    private static final String TAG = "LockScreenWindowManager";

    private static PinLockFloatWindow pinLockFloatWindow;
    private static WindowManager.LayoutParams pinLockWindowParams;
    private static WindowManager mWindowManager;
    private static boolean isLockScreenAvailable = false;
    private static CustomDialog permissionDialog;

    public static void createLockFloatWindow(Context context) {
        if(context == null){
            Log.e(TAG,"context == null");
            return;
        }

        if(mWindowManager == null){
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }

        if(pinLockWindowParams == null){
            pinLockWindowParams = new WindowManager.LayoutParams();
        }

        if(pinLockFloatWindow == null){
            pinLockFloatWindow = new PinLockFloatWindow(context);
        }else {
            pinLockFloatWindow.setForgetGone();
        }

        pinLockFloatWindow.clearPwd();
        pinLockWindowParams.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        // 设置window type
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            pinLockWindowParams.type = WindowManager.LayoutParams.TYPE_TOAST;
//        } else {
//            pinLockWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
//        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){//8.0
            pinLockWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else {
            pinLockWindowParams.type =  WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        pinLockWindowParams.format = PixelFormat.TRANSPARENT;
        pinLockWindowParams.gravity = Gravity.TOP;
        pinLockWindowParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN |
                // this is to enable the notification to recieve touch events
                /*WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |*/
                // Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        pinLockWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        pinLockWindowParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        //强制竖屏
        pinLockWindowParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

        Log.i(TAG,"createLockFloatWindow()");
        if(FloatPermissionUtil.checkPermission(context)){
            if(isLockScreenAvailable){
                try {
                    mWindowManager.updateViewLayout(pinLockFloatWindow, pinLockWindowParams);
                }catch (IllegalArgumentException e){
                    e.printStackTrace();
                    mWindowManager.addView(pinLockFloatWindow, pinLockWindowParams);
                }

            }else {
                try {
                    mWindowManager.addView(pinLockFloatWindow, pinLockWindowParams);
                }catch (IllegalStateException e){
                    e.printStackTrace();
                    mWindowManager.removeView(pinLockFloatWindow);
                    mWindowManager.addView(pinLockFloatWindow, pinLockWindowParams);
                }

            }

            isLockScreenAvailable = true;
        }else {
            FloatToast.makeText(context,"书山宝您的手机没有授予悬浮窗权限，请开启后再试",5).show();
        }
    }

    public static void removeLockFloatWindow(Context context) {
        Log.i(TAG,"removeLockFloatWindow()");
        if(mWindowManager == null){
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(isLockScreenAvailable){
            try {
                mWindowManager.removeView(pinLockFloatWindow);
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }

        }
        TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        if(telephonyManager.getCallState() != TelephonyManager.CALL_STATE_RINGING) {
            isLockScreenAvailable = false;
        }
    }

    public static boolean isLockScreenAvailable() {
        return isLockScreenAvailable;
    }

    public static void setLockScreenAvailable(Boolean bool){
        isLockScreenAvailable = bool;
    }

    private static void showFloatDialog(final Context context) {
        Log.i(TAG,"showFloatDialog()");

        if(permissionDialog == null){
            permissionDialog = new CustomDialog.Builder(context)
                    .setTitle("温馨提示")
                    .setMessage("书山宝发现您的手机没有授予悬浮窗权限，请开启后再试")
                    .setNegativeButton("暂不开启", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();


                        }
                    })
                    .setPositiveButton("现在去开启", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();

                            BaseActivity baseActivity = (BaseActivity) ActivityStackManager.getActivityStackManager().currentActivity();
                            if(baseActivity != null){
                                baseActivity.setAllowBack(true);
                            }else {
                                Log.e(TAG,"baseActivity == null");
                            }
                            RomType romType = RomUtils.getRomType(context);
                            if(romType == FUNTOUCHOS_ROM){
                                openVivoManager(context);
                            }else {
                                FloatPermissionUtil.applyPermission(true,context);
                            }

                        }
                    }).create();

            Window window = permissionDialog.getWindow();
            window.setType(WindowManager.LayoutParams.TYPE_TOAST);
        }

        if(!permissionDialog.isShowing()){
            permissionDialog.show();
        }
    }

}
